﻿using Abp.Domain.Repositories;
using Abp.EntityFrameworkCore.Repositories;
using Abp.UI;
using Microsoft.EntityFrameworkCore;
using SOEI.Solution.CommonOthers.Pdf.Enum;
using SOEI.Solution.CommonOthers.Pdf.StructEntities;
using SOEI.Solution.Helper;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SOEI.Solution.CommonOthers.Pdf.DomainService
{
    public class PdfManager : DomainServiceBase, IPdfManager
    {
        public readonly IRepository<Default> _repository;

        public readonly IRepository<PDFContent, string> _pdfRepository;

        public PdfManager(IRepository<Default> repository, IRepository<PDFContent, string> pdfRepository)
        {
            _repository = repository;
            _pdfRepository = pdfRepository;
        }

        public async Task<byte[]> GetPdfAsync(PdfParam pParam)
        {
            if(pParam == null)
            {
                throw new UserFriendlyException("参数不能为空！");
            }

            PDFContent vPdf = null;

            if (string.IsNullOrWhiteSpace(pParam.Id))
            {
                if (pParam.YWLX == PdfType.未知)
                {
                    throw new UserFriendlyException("业务类型不能为空！");
                }

                if (string.IsNullOrWhiteSpace(pParam.YWBH))
                {
                    throw new UserFriendlyException("业务编号不能为空！");
                }

                var vYWLX = Convert.ToString((int)pParam.YWLX);

                vPdf = await _pdfRepository.GetAll().AsNoTracking()
                    .Where(a => a.YWLX == vYWLX && a.YWBH == pParam.YWBH)
                    .OrderByDescending(a => a.LRSJ).FirstOrDefaultAsync();
            }
            else
            {
                vPdf = await _pdfRepository.GetAsync(pParam.Id);
            }

            if(vPdf != null)
            {
                return vPdf.WJNR;
            }

            return Convert.FromBase64String("");
        }

        public async Task<string> Create(PdfParam pParam)
        {
            if(pParam == null)
            {
                throw new UserFriendlyException("参数不能为空！");
            }

            if (string.IsNullOrWhiteSpace(pParam.Html))
            {
                throw new UserFriendlyException("要生成Pdf的Html内容不能为空！");
            }

            if (string.IsNullOrWhiteSpace(pParam.Id))
            {
                if (pParam.YWLX == PdfType.未知)
                {
                    throw new UserFriendlyException("业务类型不能为空！");
                }

                if (string.IsNullOrWhiteSpace(pParam.YWBH))
                {
                    throw new UserFriendlyException("业务编号不能为空！");
                }

                pParam.Id = Guid.NewGuid().ToString().Replace("-", "");
            }

            var vData = PDFHelper.This.CreatePDF(pParam.Html, pParam.ShowPager, pParam.PageFormat);

            var vYWLX = Convert.ToString((int)pParam.YWLX);

            var vParam = ParamCollect.FromObj(new {
                IS_ID = pParam.Id,
                IS_WJNR = vData,
                IS_YWLX = vYWLX,
                IS_YWBH = pParam.YWBH
            });

            await _pdfRepository.GetDbContext().Database.ExecuteStoreProcedure(Package.PKG_NESF_OTHERS + "SP_INS_PDF", vParam);

            return pParam.Id;
        }

        /// <summary>判断是否存在PDF文件
        /// </summary>
        public async Task<bool> IsExistsPdf(PdfParam pParam)
        {
            if(pParam == null)
            {
                throw new UserFriendlyException("参数不能为空！");
            }

            if(pParam.YWLX == PdfType.未知)
            {
                throw new UserFriendlyException("业务类型不能为空！");
            }

            if (string.IsNullOrWhiteSpace(pParam.YWBH))
            {
                throw new UserFriendlyException("业务编号不能为空！");
            }

            var vYWLX = Convert.ToString((int)pParam.YWLX);

            return await _pdfRepository.GetAll().AsNoTracking().AnyAsync(a=> a.YWLX == vYWLX && a.YWBH == pParam.YWBH);
        }
    }
}
